<template>
  <el-dialog
    :title="'新增'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="70%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="140px">
      <el-form-item label="真实姓名" prop="realname" label-width="140px">
        <el-input v-model="dataForm.realname" placeholder="真实姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile" label-width="140px">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="账户余额" prop="acount" label-width="140px">
        <el-input v-model="dataForm.acount" placeholder="账户余额"></el-input>
      </el-form-item>
      <el-form-item label="违章次数" prop="unlowCount" label-width="140px">
        <el-input v-model="dataForm.unlowCount" placeholder="违章次数"></el-input>
      </el-form-item>
      <el-form-item label="违章未处次数" prop="unlowCountUnhandler" label-width="140px">
        <el-input v-model="dataForm.unlowCountUnhandler" placeholder="违章未处次数"></el-input>
      </el-form-item>
      <el-form-item label="可用分数" prop="useScore" label-width="140px">
        <el-input v-model="dataForm.useScore" placeholder="可用分数"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" :disabled="submitVisible" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import axios from 'axios'
  import vueWangeditor from 'vue-wangeditor'
  export default {
    data () {
      return {
        visible: false,
        submitVisible: false,
        dataForm: {
          uid: '',
          realname: '',
          mobile: '',
          acount: '',
          unlowCount: '',
          unlowCountUnhandler: '',
          totalScore: '',
          useScore: ''
        },
        dataRule: {
          realname: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          acount: [
            { required: true, message: '账户余额不能为空', trigger: 'blur' }
          ],
          unlowCount: [
            { required: true, message: '违章次数不能为空', trigger: 'blur' }
          ],
          unlowCountUnhandler: [
            { required: true, message: '违章未处次数不能为空', trigger: 'blur' }
          ],
          totalScore: [
            { required: true, message: '已记分数不能为空', trigger: 'blur' }
          ],
          useScore: [
            { required: true, message: '可用分数不能为空', trigger: 'blur' }
          ]
        },
        editorUrl: ''
      }
    },
    components: {
      vueWangeditor
    },
    methods: {
      init (id) {
        this.visible = true
        this.submitVisible = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.submitVisible = true
            this.$http({
              url: this.$http.adornUrl(`/gomoney-auth/t/user/${!this.dataForm.uid ? 'add' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'realname': this.dataForm.realname,
                'mobile': this.dataForm.mobile,
                'acount': this.dataForm.acount,
                'unlowCount': this.dataForm.unlowCount,
                'unlowCountUnhandler': this.dataForm.unlowCountUnhandler,
                'totalScore': this.dataForm.totalScore,
                'useScore': this.dataForm.useScore
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      loadImg (originalName, resultText) {
        let a = JSON.parse(resultText)
        if (a && a.code === 0) {
          var url = this.openImg(a.url)
          this.$refs.wangeditor.insertImg(url)
        }
      },
      openImg (url) {
        if (url) {
          url = this.$http.adornUrl(`/pig-file/center/file/openImg?url=${url}`)
          return url
        } else {
          return null
        }
      },
      getInfo () {
        this.dataForm.noticeContent = this.$refs.wangeditor.getHtml()
      },
      handleEditerSuccess (res, file, fileList) {
        if (res && res.code === 0) {
          let url = res.url
          let param = `?url=${url}`
          axios.post(this.$http.adornUrl(`/pig-file/center/file/player` + param),
          {url: url}, {responseType: 'blob'})
            .then(response => {
              let urls = window.URL.createObjectURL(response.data)
              this.$refs.wangeditor.appendHtml(`<p><iframe src='` + urls + `' allowfullscreen='true' webkitallowfullscreen='true' mozallowfullscreen='true'></iframe></p>`)
            })
        } else {
          this.$message.error(res.msg)
        }
      }
    }
  }
</script>

<style>
</style>
